﻿(function () {
    "use strict";

    var calendar = [
        [new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List()],
        [new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List()],
        [new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List()],
        [new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List()],
        [new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List()],
        [new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List()],
        [new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List(), new WinJS.Binding.List()]];

    WinJS.UI.Pages.define("/pages/calendar/weekCalendar/weekCalendar.html", {
        ready: function (element, options) {
            
            DateUtility.CurrentMonth = options.item.monthInt;

            var i = 0;
            var j = 0;
            var item = null;

            for (i = 0; i < 6; i++) {
                for (j = 0; j < 7; j++) {
                    item = document.querySelector("#Calendar_item_" + eval(i + 1) + "_" + j);
                    MyAnimation.PointerThemeAnimation(item);
                    item.addEventListener("click", itemCalendarClick(item));
                }
            }

            initPage();

            document.querySelector("#firstSubHeader").addEventListener("click", function () {
                var date = new Date(DateUtility.CurrentYear, DateUtility.CurrentMonth).addMonths(-1);
                DateUtility.CurrentMonth = date.getMonth();
                DateUtility.CurrentYear = date.getFullYear();
                initPage();
            });
            document.querySelector("#thirdSubHeader").addEventListener("click", function () {
                var date = new Date(DateUtility.CurrentYear, DateUtility.CurrentMonth).addMonths(1);
                DateUtility.CurrentMonth = date.getMonth();
                DateUtility.CurrentYear = date.getFullYear();
                initPage();
            });
        }
    });

    var initPage = function () {
        var date = new Date(DateUtility.CurrentYear, DateUtility.CurrentMonth);

        document.querySelector("#secondSubHeader").innerText = date.toString("MMMM - yyyy");
        

        var i = 0;
        var j = 0;

        for (i = 0; i < 6; i++) {
            for (j = 0; j < 7; j++) {
                calendar[i][j].splice(0, calendar[i][j].length);
                document.querySelector("#Calendar_item_" + eval(i + 1) + "_" + j + " .dayOfWeek").innerText = "";
                document.querySelector("#Calendar_item_" + eval(i + 1) + "_" + j).style.display = "none";

                // mise a jour du calendar
                //TODO

                var day = date.getDay() - 1;
                if (day == -1) day = 6;

                if (day == j && date.getMonth() == DateUtility.CurrentMonth) {
                    document.querySelector("#Calendar_item_" + eval(i + 1) + "_" + j).style.display = "block";
                    document.querySelector("#Calendar_item_" + eval(i + 1) + "_" + j + " .dayOfWeek").innerText = date.getDate();
                    date.addDays(1);
                }
            }
        }
    }


    var itemCalendarClick = function(item){
        return function () {
            WinJS.Navigation.navigate('/pages/calendar/weekDetailCalendar/weekDetailCalendar.html', { day: item.querySelector(".dayOfWeek").innerText });
        }
    }
    
    var publicMembres = {
        calendar : calendar
    };


    WinJS.Namespace.define("WeekCalendar", publicMembres);

})();
